/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.mallow.impl.model.api;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Transient;

import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import javax.persistence.Enumerated;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;

import org.hibernate.annotations.GenericGenerator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jiscinvolve.astrodabis.mallow.model.api.Tag;
import org.jiscinvolve.astrodabis.mallow.model.api.Query;
import org.jiscinvolve.astrodabis.mallow.model.api.Result;
import org.jiscinvolve.astrodabis.mallow.model.api.Account;

import org.jiscinvolve.astrodabis.mallow.model.tap.TapTable;
import org.jiscinvolve.astrodabis.mallow.model.tap.TapObject;
import org.jiscinvolve.astrodabis.mallow.model.tap.TapService;

import org.jiscinvolve.astrodabis.mallow.util.GenericIterable;

/**
 * Implementation of the Result bean.
 *
 */
@Entity
@Table(name="results")
public class ResultImpl
implements Result
    {
    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(ResultImpl.class);

    /**
     * Protected constructor.
     *
     */
    protected ResultImpl()
        {
        super();
        }

    /**
     * Reference to self for inner classes..
     *
     */
    protected ResultImpl self()
        {
        return this ;
        }

    /**
     * Our parent Query.
     *
     */
    @ManyToOne
    @JoinColumn(name="query", nullable=false)
    private QueryImpl query ;

    @Override
    public QueryImpl query()
        {
        return this.query ;
        }

    @Override
    public Account owner()
        {
        if (this.query != null)
            {
            return this.query.owner() ;
            }
        else {
            return null ;
            }
        }

    @Override
    public TapTable table()
        {
        if (this.query != null)
            {
            return this.query.table() ;
            }
        else {
            return null ;
            }
        }

    @Override
    public TapService service()
        {
        if (this.query != null)
            {
            return this.query.service() ;
            }
        else {
            return null ;
            }
        }

    /**
     * The object identifier (within the TapTable).
     *
     */
    @Column(name="oid")
    private String oid ;

    @Override
    public String oid()
        {
        return this.oid ;
        }

    /**
     * The row number (within the Query results).
     *
     */
    @Column(name="row")
    private Long row ;

    @Override
    public Long row()
        {
        return this.row ;
        }

    /**
     * The result identifier (within AstroDAbis).
     *
     */
    @Id
    @Column(name="ident")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long ident ;

    @Override
    public Long ident()
        {
        return this.ident ;
        }

    /**
     * The corresponding object in the TapTable.
     *
     */
    @Transient
    private TapObject object ;

    @Override
    public TapObject object()
        {
        if (this.object == null)
            {
            this.object = new TapObject(
                this.table(),
                this.oid
                );
            }
        return this.object ;
        }

    }

